package org.vnl.platform.orm;

import java.text.DecimalFormat;

import org.vnl.platform.dk.util.SEVERITY;

/**
 * 執行Transaction時的錯誤代碼
 * 
 * @author Vaughan
 *
 */
public enum TransactionErrCode {

    /**
     * Transaction已經起動
     */
    TRANSACTION_ALREADY_BEGAN(SEVERITY.EXCEPTION),
    /**
     * Transaction尚未起動
     */
    TRANSACTION_NOT_BEGAN(SEVERITY.EXCEPTION),
    /**
     * SQL Exception occured params-> 0: SQL Exception reason
     */
    ERR_SQL_EXCEPTION(SEVERITY.EXCEPTION),
    /**
     * 沒有SQL可執行
     */
    ERR_SQL_MISSING(SEVERITY.FAIL),
    /**
     * 沒有指定回傳的類別
     */
    ERR_RESULT_CLASS_MISSING(SEVERITY.FAIL),
    /**
     * 還沒設定目前要使用的PM
     */
    ERR_CURRENT_PM_NOT_SET(SEVERITY.FAIL),
    /**
     * 執行查詢錯誤
     * 0-> 原錯誤的錯誤原因
     */
    ERR_EXECUTE_QUERY(SEVERITY.EXCEPTION),
    /**
     * 執行更新錯誤
     * 0-> 原錯誤的錯誤原因
     */
    ERR_EXECUTE_UPDATE(SEVERITY.EXCEPTION),
    /**
     * 交易只能Rollback
     * 
     */
    TRANSACTION_MUST_ROLLBACK(SEVERITY.ERROR),
    /**
     * 交易COMMIT時發生錯誤
     */
    TRANSACTION_COMMIT_ERROR(SEVERITY.ERROR),
    /**
     * 交易ROLLBACK時發生錯誤
     */
    TRANSACTION_ROLLBACK_ERROR(SEVERITY.ERROR);
    

    SEVERITY severity;
    
    TransactionErrCode(SEVERITY severity) {
        this.severity = severity;
    }
    
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("TX_"); // Transaction
        DecimalFormat df = new DecimalFormat("0000");
        sb.append(df.format(this.ordinal()));
        return sb.toString();
    }
    
    public String getSeverityString() {
        return this.severity.toString();
    }
    
    public SEVERITY getSeverity() {
        return this.severity;
    }
    
    public static void main(String[] args) throws Exception {
        
        TransactionErrCode[] values = TransactionErrCode.values();
        for (int ii=0; ii<values.length; ii++) {
            System.out.println("#"+values[ii].name());
            System.out.println(values[ii].toString()+"=");
            System.out.println("");
        }
    }

}
